<?php

namespace common\models;

use Yii;
use yii\base\Object;
use common\extended\system\Util;

/**
 * This is the model class for table "bus_repertory".
 *
 * @property string $repertory_id
 * @property string $repertory_name
 * @property string $repertory_specs
 * @property integer $repertory_count
 * @property string $supplier_name
 * @property string $repertory_brand
 * @property string $repertory_series
 * @property string $repertory_category
 * @property string $shop_id
 * @property string $boss_id
 * @property integer $is_sell
 * @property string $bar_code
 * @property string $goods_sell_price
 * @property string $goods_cost_price
 */
class BusRepertory extends \yii\db\ActiveRecord
{
    //错误信息
    private $errorMessage;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'bus_repertory';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['repertory_name', 'repertory_specs', 'supplier_name', 'repertory_brand', 'repertory_series', 'repertory_category', 'shop_id', 'boss_id', 'bar_code'], 'required'],
            [['repertory_count', 'shop_id', 'boss_id', 'is_sell'], 'integer'],
            [['goods_sell_price', 'goods_cost_price'], 'number'],
            [['repertory_name', 'supplier_name', 'bar_code'], 'string', 'max' => 255],
            [['repertory_specs', 'repertory_brand', 'repertory_series', 'repertory_category'], 'string', 'max' => 45]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'repertory_id' => 'Repertory ID',
            'repertory_name' => '商品名称',
            'repertory_specs' => '商品规格',
            'repertory_count' => '剩余库存量',
            'supplier_name' => '所属供应商',
            'repertory_brand' => '品牌',
            'repertory_series' => '系列',
            'repertory_category' => '分类',
            'shop_id' => '所属店铺',
            'boss_id' => '老板ID',
            'is_sell' => '是否家居产品    1：是  0：否',
            'bar_code' => '条形码',
            'goods_sell_price' => '零售价',
            'goods_cost_price' => '成本价',
        ];
    }
    
    /*模块交互接口  */
    /**
     * 修改库存数量(售卖家居产品)
     * @date: 2016-2-22
     * @author: futao
     * @param integer $repertory_id 库存(商品)id
     * @param integer $count 出库数量
     * @param integer $shop_id 分店ID
     * @param integer $boss_id 老板ID
     * @param integer $uid 登录人ID
     */
    public function updateRepertorycount($repertory_id,$count,$shop_id,$boss_id,$uid){
        if(intval($repertory_id) < 1 || intval($count) < 1 || intval($shop_id) < 1 || intval($boss_id) < 1 || intval($uid) < 1){
            $this->setErrormessage('参数错误！');
            return false;
        }
        //开启事务
        $transAction = \Yii::$app->db->beginTransaction();
        try {
            $repertory = BusRepertory::find()->where('repertory_id = :id and is_sell = 1',[':id' => $repertory_id])->one();
            if(!$repertory){
                $this->setErrormessage('商品不存在！');
                return false;
            }
            //修改库存数量
            $repertory->repertory_count = $repertory->repertory_count - $count;
            $repertory->save(false);
            //添加出库记录
            $this->addGooddeclog($count,$repertory,$shop_id,$boss_id,$uid);
            //提交
            $transAction->commit();
            return true;
        } catch (\Exception $e) {
            //回滚
            $transAction->rollBack();
            $this->setErrormessage('修改库存数量失败！');
            return false;
        }
        
    }
    /**
     * 添加出库记录
     * @date: 2016-2-22
     * @author: futao
     */
    public function addGooddeclog($count,$repertory,$shop_id,$boss_id,$uid){
        //获取唯一标识号
        $unique_flag = Util::getSerialNumber($uid);
        $goodDec = new BusGoodsDec();
        $goodDec->dec_type = 5;
        $goodDec->dec_count = $count;
        $goodDec->goods_name = $repertory['repertory_name'];
        $goodDec->supplier_name = empty($repertory['supplier_name']) ? '' : $repertory['supplier_name'];
        $goodDec->dec_remark = '售卖家居产品';
        $goodDec->dec_time = date('Y-m-d H:i:s');
        $goodDec->shop_id = $shop_id;
        $goodDec->boss_id = $boss_id;
        $goodDec->unique_flag = $unique_flag;
        $goodDec->goods_specs = empty($repertory['repertory_specs']) ? '' : $repertory['repertory_specs'];
        $goodDec->repertory_count = $repertory['repertory_count'];
        $goodDec->goods_sell_price = $repertory['goods_sell_price'];
        $goodDec->repertory_id = $repertory['repertory_id'];
        $goodDec->save(false);
    }
    /**
     * 添加家居产品
     * @date: 2016年3月3日 
     * @author: chenxiaolin
     * @param array $data
     * @param $data['shopId'] 分店ID
     * @param $data['bossId'] 老板ID
     * @param $data['uid'] 登录人ID
     * @param $data['repertory_name'] 商品名称
     * @param $data['repertory_count'] 数量
     * @param $data['goods_sell_price'] 零售价
     */
    public function addRepertory($data){
        if(intval($data['shopId']) < 1 || intval($data['bossId']) < 1){
            $this->setErrormessage('ID错误！');
            return false;
        }
        if(!is_array($data)){
            $this->setErrormessage('data为数组！');
            return false;
        }
        if($data['repertory_name'] == '' || $data['repertory_count'] == '' || !is_numeric($data['repertory_count'])){
            $this->setErrormessage('参数错误');
            return false;
        }
        $transAction = \Yii::$app->db->beginTransaction();
        try {
            //添加产品
            $repertory = new BusRepertory();
            $repertory->repertory_name = $data['repertory_name'];
            $repertory->repertory_count = $data['repertory_count'];
            $repertory->goods_sell_price = $data['goods_sell_price'];
            $repertory->shop_id = $data['shopId'];
            $repertory->boss_id = $data['bossId'];
            $repertory->is_sell = 1;
            $repertory->save(false);
            //添加入库记录
            $this->addRepertoryinclog($data);
            $transAction->commit();
            return true;
        } catch (\Exception $e) {
            $transAction->rollBack();
            $this->setErrormessage('添加失败！');
            return false;
        }
        
    }
    
    /**
     * 添加入库记录
     * @date: 2016年3月4日 
     * @author: chenxiaolin
     */
    public function addRepertoryinclog($data){
        //获取唯一标识号
        $unique_flag = Util::getSerialNumber($data['uid']);
        $repertoryInc = new BusRepertoryInc();
        $repertoryInc->goods_name = $data['repertory_name'];
        $repertoryInc->goods_sell_price = $data['goods_sell_price'];
        $repertoryInc->inc_remark = '';
        $repertoryInc->inc_time = date('Y-m-d H:i:s');
        $repertoryInc->shop_id = $data['shopId'];
        $repertoryInc->boss_id = $data['bossId'];
        $repertoryInc->unique_flag = $unique_flag;
        $repertoryInc->repertory_count = $data['repertory_count'];
        $repertoryInc->inc_count = $data['repertory_count'];
        $repertoryInc->save(false);
    }
    
    /**
     * 修改家居产品
     * @date: 2016年3月4日
     * @author: chenxiaolin
     * @param integer $repertory_id 库存(商品)id
     * @param integer $shop_id 店铺id
     * @param $data['repertory_name'] 商品名称
     * @param $data['goods_sell_price'] 零售价
     */
    public function editRepertory($repertory_id,$shop_id,$data){
        if(intval($repertory_id < 1) || intval($shop_id < 1) || !is_array($data) || empty($data['repertory_name'])){
            $this->setErrormessage('参数错误！');
            return false;
        }
        $repertory = BusRepertory::find()->where('repertory_id = :id and shop_id = :shop_id and is_sell = 1',[':id' => $repertory_id,':shop_id' => $shop_id])->one();
        if(!$repertory){
            $this->setErrormessage('商品不存在！');
            return false;
        }
        //修改商品
        $repertory->repertory_name = $data['repertory_name'];
        $repertory->goods_sell_price = $data['goods_sell_price'];
        if($repertory->save(false)){
            return true;
        }else{
            $this->setErrormessage('修改失败');
            return false;
        } 
    }
    /*end  */
    
    /**
     * 设置错误信息
     * @date: 2016年2月29日 
     * @author: chenxiaolin
     */
    public function setErrormessage($message){
        $this->errorMessage = $message;
    }
    
    /**
     * 获取错误信息
     * @date: 2016年2月29日 
     * @author: chenxiaolin
     */
    public function getErrormessage(){
        return $this->errorMessage;
    }
}
